#pragma once
#include <iostream>
#include <string>
#include <unordered_map>
#include <cassert>
#include <fstream>
#include <vector>
#include "../Common/log.hpp"
#include "../Common/util.hpp"
#include "./include/mysql.h"

namespace ns_model
{
    using namespace ns_log;
    using namespace ns_util;
    //MVC结构。M获取数据信息
    struct Question
    {
        std::string _number;//题目编号
        std::string _title;//题目标题
        std::string _level;//题目难度
        int _cpu_limit;//题目cpu限制
        int _mem_limit;//题目内存限制

        std::string _desc;//文件描述
        std::string _header;//预设代码
        std::string _tail;//测试代码
    };
    const std::string oj_questions = "oj_questions";
    const std::string host = "127.0.0.1";
    const std::string user = "oj_client";
    const std::string passwd = "123456";
    const std::string db = "oj";
    const int port = 3306;
    class Model
    {
    public:
        Model()
        {
        }
        ~Model()
        {}
        bool QueryMySql(const std::string &sql,vector<Question>* v)
        {
            //创建mysql句柄
            MYSQL *my = mysql_init(nullptr);
            if(nullptr == mysql_real_connect(my,host.c_str(),user.c_str(),passwd.c_str(),db.c_str(),port,nullptr,0))
            {
                LOG(FATAL)<<"连接数据库失败！"<<"\n";
                return false;
            }
            mysql_set_character_set(my,"utf8");//设置编码格式
            LOG(INFO)<<"连接数据库成功!"<<"\n";
            //执行sql语句
            if(0 != mysql_query(my,sql.c_str()))
            {
                LOG(WARNING) << sql <<"execute error!" <<"\n";
                return false;
            }
            //提取结果
            MYSQL_RES *res = mysql_store_result(my);
            //分析结果
            int rows = mysql_num_rows(res);

            for (int i = 0; i < rows; i++)
            {
                MYSQL_ROW row = mysql_fetch_row(res);
                Question q;
                q._number = row[0];
                q._title = row[1];
                q._level = row[2];
                q._desc = row[3];
                q._header = row[4];
                q._tail = row[5];
                q._cpu_limit = atoi(row[6]);
                q._mem_limit = atoi(row[7]);

                v->push_back(q);
            }
            //释放结果空间
            free(res);
            //关闭mysql连接
            mysql_close(my);
        }
        //获取当前题目列表信息
        bool GetAllQestions(std::vector<Question>* v)
        {
            std::string sql = "select * from";
            sql += oj_questions;
            return QueryMySql(sql,v);
        }
        //获取指定编号题目
        bool GetOneQuestion(const std::string number,Question* q)
        {
           bool res = false;
           std::string sql = "select * from ";
           sql += oj_questions;
           sql += " where number=";
           sql += number;
           vector<Question> result;
           if(QueryMySql(sql, &result))
           {
                if(result.size() == 1)
                {
                    *q = result[0];
                    res = true;
                }
           }
           return res;
        }
    };
}